﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Common.Resources;
using nyxwin.Common.Base.Common;
using System.Collections.Generic;
using System.Linq;
using nyxwin.Common.Settings;
using System.Threading;

namespace nyxwin.Common.Providers
{
    public sealed class LocalizationProvider : NotifyingObject
    {
        #region Instance
        private static object _lock = new object();
        private static LocalizationProvider _instance = null;

        public static LocalizationProvider Instance
        {
            get
            {
                lock (_lock)
                {
                    if (_instance == null)
                        _instance = new LocalizationProvider();

                    return _instance;
                }
            }
        }

        private LocalizationProvider()
        {
            Init();
        }
        #endregion

        #region Construction
        private void Init()
        {
            Resource = new LocalizationResource();
            SetCurrentLanguage();
        }
        #endregion

        #region Resource
        public LocalizationResource Resource { get; private set; }

        public string GetString(string key)
        {
            var val = LocalizationResource.ResourceManager.GetString(key);
            return val;
        }

        public string GetErrorString(string key)
        {
            var val = ErrorResource.ResourceManager.GetString(key);
            return val;
        }
        #endregion

        #region Languages
        private string[] _availableLanguages = new string[] 
        {
            "en-US",
            "cs-CZ"
        }; 

        public IEnumerable<string> AvailableLanguages
        {
            get
            {
                return _availableLanguages;
            }
        }

        public string CurrentLanguage
        {
            get
            {
                var key = ApplicationSettings.Instance.Load("languageKey", string.Empty);
                var lang = AvailableLanguages.FirstOrDefault(x => x == key);
                if (lang == null)
                    lang = AvailableLanguages.First();
                return lang;
            }
            set
            {
                if (value != null)
                {
                    ApplicationSettings.Instance.Save("languageKey", value);
                    RaisePropertyChanged(() => CurrentLanguage);

                    SetCurrentLanguage();
                }
            }
        }

        private void SetCurrentLanguage()
        {
            var curLan = CurrentLanguage;

            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(curLan);
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(curLan);

            RaisePropertyChanged(() => Resource);
        }
        #endregion
    }
}
